package com.abei.gas.service.Impl;

import com.abei.gas.entity.Card;
import com.abei.gas.entity.User;
import com.abei.gas.mapper.UserMapper;
import com.abei.gas.service.UserService;
import com.abei.gas.vo.DataVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper usermapper;
    @Override
    public DataVo<User> findAll(Integer page,Integer limit) {
        List<User> c=usermapper.selectList(null);
        DataVo<User> datavo=new DataVo<>();
        datavo.setCode(0);
        datavo.setMsg("");
        IPage<User> cardpage=new Page<>(page,limit);
        IPage<User> result=usermapper.selectPage(cardpage,null);
        datavo.setCount(result.getTotal());
        c=result.getRecords();
        datavo.setData(c);
        return datavo;
    }

    @Override
    public void deleteById(String userid) {
        Map<String,Object> columMap=new HashMap<>();
        columMap.put("id",userid);
        int i=usermapper.deleteByMap(columMap);
        System.out.println(i);
    }

    @Override
    public void Insert(User user) {
        User u=new User();
        u.setId(user.getId());
        u.setName(user.getName());
        u.setAddress(user.getAddress());
        u.setPhone(user.getPhone());
        usermapper.insert(u);
    }

    @Override
    public DataVo<User> findById(String number) {
        QueryWrapper<User> u = new QueryWrapper<>();
        u.like("id",number);
        List<User> cardList=usermapper.selectList(u);
        DataVo<User> dataVO=new DataVo<>();
        dataVO.setCode(0);
        dataVO.setMsg("");
        Integer num = usermapper.selectCount(null);
        dataVO.setCount(Long.valueOf(num));
        dataVO.setData(cardList);
        return dataVO;
    }

    @Override
    public void Update(String number, User user) {
        QueryWrapper<User> u=new QueryWrapper<User>();
        u.eq("id",number);
        usermapper.update(user,u);
    }
}
